package edu.whu.puppygo.service.Impl;

import edu.whu.puppygo.assistant.ClassificationAssistant;
import edu.whu.puppygo.dao.PetMatterDao;
import edu.whu.puppygo.dto.PetMatterDto;
import edu.whu.puppygo.entity.PetMatter;
import edu.whu.puppygo.entity.PetMatterClassificationEnum;
import edu.whu.puppygo.exception.BackendException;
import edu.whu.puppygo.service.PetMatterService;
import edu.whu.puppygo.util.GlobalUser;
import edu.whu.puppygo.util.VoConvertUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.temporal.ChronoUnit;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author xinjiezhuang
 * @Date 2023-11-20
 */
@Service
@Slf4j
public class PetMatterServiceImpl implements PetMatterService {
    @Resource
    private PetMatterDao petMatterDao;

    @Resource
    private ClassificationAssistant agent;

    // 添加PetMatter
    @Override
    public PetMatter addPetMatter(PetMatterDto petMatterDto) {
        log.info("petMatterDto: {}", petMatterDto);
        Long userId = GlobalUser.getUser().getId();
        PetMatter petMatter = VoConvertUtil.convertEntity(petMatterDto, PetMatter.class);
        log.info("petMatter: {}", petMatter);
        petMatter.setUserId(userId);
        PetMatterClassificationEnum petMatterClassificationEnum = agent.getPetMatterClassificationEnum(petMatter.getTitle() + petMatter.getContent());
        log.info("petMatterClassificationEnum: {}", petMatterClassificationEnum);
        petMatter.setClassification(petMatterClassificationEnum);
        return petMatterDao.save(petMatter);
    }

    // 删除PetMatter
    @Override
    public void finishPetMatter(Long petMatterId) {
        PetMatter petMatter = petMatterDao.findById(petMatterId).orElseThrow(() -> new BackendException("PetMatter not found"));
        petMatter.setDeleteTime(LocalDateTime.now());
        petMatterDao.save(petMatter);
    }

    // 修改PetMatter
    @Override
    public PetMatter updatePetMatter(PetMatter petMatter) {
        Long userId = GlobalUser.getUser().getId();
        petMatter.setUserId(userId);
        return petMatterDao.save(petMatter);
    }

    // 根据用户ID获取所有的PetMatter
    @Override
    public List<PetMatter> getAllPetMattersByUserId(Long userId, Pageable pageable) {
        return petMatterDao.findByUserIdAndDeleteTimeNull(userId, pageable);
    }

    //返回今天应该执行的PetMatter
    @Override
    public List<PetMatter> getTodayPetMattersByUserId(Long userId, Pageable pageable) {
        return petMatterDao.findTodayPetMatters(userId, pageable);
    }

    // 根据用户ID和日期获取所有的PetMatter
    @Override
    public List<PetMatter> findPetMattersByUserIdAndDate(Long userId, LocalDate date) {
        List<PetMatter> matters = petMatterDao.findByDeleteTimeNullAndUserIdOrderByUpdateTimeDesc(userId);
        List<PetMatter> res = new ArrayList<>();
        for (PetMatter matter : matters) {
            // 只执行一次的事务
            if (matter.isOnlyOnce() && matter.getStartTime().equals(date)) {
                res.add(matter);
                continue;
            }
            // 周期执行的事务
            if (ChronoUnit.DAYS.between(matter.getStartTime(), date) % matter.getImplementationCycleByDay() == 0) {
                res.add(matter);
            }
        }
        return res;
    }
}
